package com.example.myapplication.Adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.example.myapplication.Bean.PyqItem;
import com.example.myapplication.PyqActivity;
import com.example.myapplication.R;

import java.util.List;

public class PyqItemAdapter extends ArrayAdapter<PyqItem> {  /*指定类型*/
    //    切换图片所需
    boolean isChanged = false;


    /*2.在写全局变量*/
    int resourceid;

    /*1.第一先构造   Ait+inset 倒数第二个 包*/
    public PyqItemAdapter(@NonNull Context context, int resource, @NonNull List<PyqItem> objects) {

        super(context, resource, objects);


        /*3.在接住 */
        this.resourceid = resource;

    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = convertView;
        if (view == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceid, null);

        }

        ImageView userimg = view.findViewById(R.id.userimg);
        TextView username = view.findViewById(R.id.username);
        TextView usercontent = view.findViewById(R.id.usercontent);
        ImageView contentimg = view.findViewById(R.id.contentimg);
        Button pyq_comment = view.findViewById(R.id.pyq_comment);

//        final  变量‘pyq_comment_txt’是从内部类中访问的，需要声明为Final。
        final TextView pyq_comment_txt = view.findViewById(R.id.pyq_comment_txt);

//        final  变量‘pyq_umbs_up’是从内部类中访问的，需要声明为Final。

        final ImageView pyq_thumbs_up = view.findViewById(R.id.pyq_thumbs_up);


        PyqItem p = getItem(position);
        userimg.setImageResource(p.getUserimg());
        username.setText(p.getUsername());
        usercontent.setText(p.getPyqcontent());
        contentimg.setImageResource(p.getPyqimg());

//点赞功能的实现
        pyq_thumbs_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v == pyq_thumbs_up) {
                    if (isChanged) {
                        pyq_thumbs_up.setImageDrawable(getContext().getDrawable(R.mipmap.dianzan8a));
                    } else {
                        pyq_thumbs_up.setImageDrawable(getContext().getDrawable(R.mipmap.pinkdianzan));
                    }
                    isChanged = !isChanged;

                }
            }
        });
//        点击评论功能
        pyq_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                所在的activity
                AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                final View v = LayoutInflater.from(getContext()).inflate(R.layout.dialog_customize, null);
                builder.setIcon(R.mipmap.bi);
                builder.setTitle("发表评论");
//                创建视图
                builder.setView(v);
                builder.setNegativeButton("取消", null);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        // 获取EditView中的输入内容
                        EditText edit_dialog = v.findViewById(R.id.edit_dialog);
//                        把 获取EditView中的输入内容 传入到TextView中
                        pyq_comment_txt.setText(edit_dialog.getText().toString());
                    }
                });

                builder.create().show();
            }
        });

        return view;


    }


}
